package com.woniu.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Date;
import java.util.Map;

//JWT工具类
public class JwtUtilOld {
    //自定义的秘钥
    //HS256：即字节长度>=32
    static String sect ="ksaasfdasdfadsfwfrasdfsadffcasijfasadfasfdlkfjlasjflksajfd";
    static String adminSect="aboiahchvisohocsmvnuauovnanvnavjvvnsvuaonvusnavn";
    //生成token
    //键值对包含用户信息
    //以分钟为单位的时间 代表jwt的有效时间
    public static String createToken(Map<String, Object> body, Integer mil) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }
    //管理员账号创建token
    public static String createAdminToken(Map<String, Object> body, Integer mil) {
        Key key = Keys.hmacShaKeyFor(adminSect.getBytes());
        return Jwts.builder()
                .setClaims(body)
                .setExpiration(new Date(System.currentTimeMillis() + mil * 60 * 1000))
                .signWith(key)
                .compact();
    }
    /*
    解析token
    正常直接返回数据
    失败
    格式错误
    签名异常
    超时异常
     */

    public static Claims parseToken(String token) {
        Key key = Keys.hmacShaKeyFor(sect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }

    //解析管理员的token
    public static Claims parseAdminToken(String token) {
        Key key = Keys.hmacShaKeyFor(adminSect.getBytes());
        return Jwts.parserBuilder().setSigningKey(key)
                .build().parseClaimsJws(token).getBody();
    }
}
